/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package UI;

import BL.BLConcepto;
import BO.Concepto;
import Utilitarios.ButtonColumn;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Daniel.Cerdas
 */
public class FrmConceptos extends javax.swing.JFrame {
    BLConcepto Conceptos = new BLConcepto();
    private DefaultTableModel ModeloTablaConceptos;
    /**
     * Creates new form FrmConceptos
     */
    public FrmConceptos() {
        initComponents();
        ModeloTablaConceptos= new DefaultTableModel(new Object[]{"DescripcionConceptos","IdConcepto","Monto","Editar"}, 0);
        TblConceptos.setModel(ModeloTablaConceptos);
        Action Modificar = new AbstractAction(){
            public void actionPerformed(ActionEvent e)
            {
                TblConceptos = (JTable)e.getSource();
                int modelRow = Integer.valueOf( e.getActionCommand() );
                Concepto conceptos = new Concepto();
                Object objeto = new Object();
                objeto = ((DefaultTableModel)TblConceptos.getModel()).getValueAt(modelRow, 0);
                String descripcion = String.valueOf(objeto);
                objeto = ((DefaultTableModel)TblConceptos.getModel()).getValueAt(modelRow, 1);
                String idConceptos = String.valueOf(objeto);
                objeto = ((DefaultTableModel)TblConceptos.getModel()).getValueAt(modelRow, 2);
                Double Monto= (Double)objeto;
                conceptos.setDescripcionConcepto(descripcion);
                conceptos.setIdConcepto(idConceptos);
                conceptos.setMonto(Monto);
                cargarModificar(conceptos);
            }
            
            };
 
        ButtonColumn buttonColumn1 = new ButtonColumn(TblConceptos, Modificar, 3);
        buttonColumn1.setMnemonic(KeyEvent.VK_D);
        cargartablaConceptos();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        BtnAgregar = new javax.swing.JButton();
        BtnModificar = new javax.swing.JButton();
        LblDescripcion = new javax.swing.JLabel();
        TxtDescripcion = new javax.swing.JTextField();
        LblIDConcepto = new javax.swing.JLabel();
        LblMonto = new javax.swing.JLabel();
        txtIDConcepto = new javax.swing.JTextField();
        TxtMonto = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        TblConceptos = new javax.swing.JTable();
        BtnCerrar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Conceptos de viaticos");
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        BtnAgregar.setFont(new java.awt.Font("Segoe UI", 0, 11)); // NOI18N
        BtnAgregar.setText("Agregar");
        BtnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnAgregarActionPerformed(evt);
            }
        });
        getContentPane().add(BtnAgregar, new org.netbeans.lib.awtextra.AbsoluteConstraints(470, 50, 100, -1));

        BtnModificar.setFont(new java.awt.Font("Segoe UI", 0, 11)); // NOI18N
        BtnModificar.setText("Modificar");
        BtnModificar.setEnabled(false);
        BtnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnModificarActionPerformed(evt);
            }
        });
        getContentPane().add(BtnModificar, new org.netbeans.lib.awtextra.AbsoluteConstraints(470, 80, 100, -1));

        LblDescripcion.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        LblDescripcion.setText("Descripcion del Concepto");
        getContentPane().add(LblDescripcion, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 120, -1, -1));

        TxtDescripcion.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        getContentPane().add(TxtDescripcion, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 110, 140, 30));

        LblIDConcepto.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        LblIDConcepto.setText("ID Concepto");
        getContentPane().add(LblIDConcepto, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 40, -1, -1));

        LblMonto.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        LblMonto.setText("Monto");
        getContentPane().add(LblMonto, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 80, -1, 16));

        txtIDConcepto.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        getContentPane().add(txtIDConcepto, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 30, 110, 30));

        TxtMonto.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        getContentPane().add(TxtMonto, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 70, 140, 30));

        TblConceptos.setFont(new java.awt.Font("Segoe UI", 0, 11)); // NOI18N
        TblConceptos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(TblConceptos);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 160, 560, 120));

        BtnCerrar.setText("Cerrar");
        BtnCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnCerrarActionPerformed(evt);
            }
        });
        getContentPane().add(BtnCerrar, new org.netbeans.lib.awtextra.AbsoluteConstraints(470, 110, 100, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BtnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnAgregarActionPerformed
        try {
            // TODO add your handling code here:
            Conceptos.AgregarConcepto(txtIDConcepto,TxtDescripcion,TxtMonto);
            JOptionPane.showMessageDialog(rootPane, "Registro agregado correctamente");
        } catch (SQLException ex) {
            Logger.getLogger(FrmConceptos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_BtnAgregarActionPerformed

    private void BtnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnModificarActionPerformed
        try {
            // TODO add your handling code here:
            Conceptos.EditarConcepto(txtIDConcepto, TxtDescripcion, TxtMonto);
        } catch (SQLException ex) {
            Logger.getLogger(FrmConceptos.class.getName()).log(Level.SEVERE, null, ex);
        }
       JOptionPane.showMessageDialog(rootPane, "Registro Registrado Correctamente");
       TxtDescripcion.setText("");
       TxtMonto.setText("");
       txtIDConcepto.setText("");
       cargartablaConceptos();
       BtnAgregar.setEnabled(true);
       BtnModificar.setEnabled(false);
    }//GEN-LAST:event_BtnModificarActionPerformed

    private void BtnCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnCerrarActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_BtnCerrarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmConceptos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmConceptos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmConceptos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmConceptos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrmConceptos().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnAgregar;
    private javax.swing.JButton BtnCerrar;
    private javax.swing.JButton BtnModificar;
    private javax.swing.JLabel LblDescripcion;
    private javax.swing.JLabel LblIDConcepto;
    private javax.swing.JLabel LblMonto;
    private javax.swing.JTable TblConceptos;
    private javax.swing.JTextField TxtDescripcion;
    private javax.swing.JTextField TxtMonto;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField txtIDConcepto;
    // End of variables declaration//GEN-END:variables

    private void cargartablaConceptos() {
        TblConceptos.removeAll();
       TblConceptos.setModel(BLConcepto.CargarTablaConceptos(ModeloTablaConceptos));
    }
    private void cargarModificar(Concepto conceptos) {
        TxtDescripcion.setText(conceptos.getDescripcionConcepto());
        TxtMonto.setText(""+conceptos.getMonto());
        txtIDConcepto.setText(conceptos.getIdConcepto());
        BtnAgregar.setEnabled(false);
        txtIDConcepto.setEditable(false);
        BtnModificar.setEnabled(true);
    }
}
